package gui.cen.tree;

import core.utils.Utils;
import gui.Segl2;
import gui.Menu;
import gui.cen.Center;
import gui.cen.south.HeatMap;
import gui.cen.read.CurrFilter;
import gui.win.tree.FullT;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.*;
import javax.swing.JViewport.*;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class TreeDisp extends JPanel implements ActionListener{
    private TreePane tp;
    private JScrollPane scr;
    private TreeOpts to;
    private JPanel search,tree;
 
    private JButton ct;
    private Segl2 segl2;
    private JLayeredPane lp;
    private CurrFilter cf;
    private Center center;
    public TreeDisp(Center center, Segl2 segl2){
        this.setLayout(new BorderLayout());
        this.center = center;
        this.segl2 = segl2;
        lp = new JLayeredPane();

        tp = new TreePane(this);
        scr = new JScrollPane(tp);
        scr.setBounds(0, 0, segl2.getWidth(), segl2.getHeight());
        lp.add(scr, new Integer(1));

        to = new TreeOpts(center);
        to.setBounds((segl2.getWidth() - (Center.WIDGE_WIDTH + Center.WIDGE_SIDE)), Center.WIDGE_TOP, Center.WIDGE_WIDTH , TreeOpts.HEIGHT_OF_OPTIONS);
        lp.add(to, new Integer(2));

        tree = Utils.getButton(ct = new JButton("Create New Tree"), 180, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Create a New Tree");
        tree.setOpaque(false);
        tree.setBounds(30, Center.WIDGE_TOP, Center.WIDGE_WIDTH, 40);
        lp.add(tree, new Integer(3));
      
        search = new JPanel(new BorderLayout());
        search.setBorder(BorderFactory.createEmptyBorder(0,5,0,5));
        search.setBounds(30, (segl2.getHeight() - (HeatMap.HEIGHT_OF_HEATMAP + 120)), Center.WIDGE_WIDTH,30);
        search.setBackground(new Color(102, 102, 102));
        search.add(new Search(center), BorderLayout.CENTER);
        lp.add(search, new Integer(4));

        cf = new CurrFilter();
        cf.setFilterLab();
        cf.setBounds((segl2.getWidth() - (Center.WIDGE_WIDTH + Center.WIDGE_SIDE)), (segl2.getHeight() - (HeatMap.HEIGHT_OF_HEATMAP + 130)), Center.WIDGE_WIDTH, CurrFilter.HEIGHT_OF_FILTER);
        lp.add(cf, new Integer(5));

        this.add(lp, BorderLayout.CENTER);
        this.addComponentListener(new ComponentAdapter(){public void componentResized(ComponentEvent e){updateTree();}});
    }

    public void msg(String msg){center.msg(msg);}
    public TreePane getTreePane(){return tp;}
    public void setFilterLVL_label(){cf.setFilterLab();}

    public void updateTree(){
        tp.updateTree();
        lp.setPreferredSize(new Dimension(segl2.getWidth()-4, segl2.getHeight() - HeatMap.HEIGHT_OF_HEATMAP - 42));
        scr.setBounds(0, 0, segl2.getWidth()-8, segl2.getHeight() - HeatMap.HEIGHT_OF_HEATMAP - 42);
        to.setBounds((segl2.getWidth() - (Center.WIDGE_WIDTH + Center.WIDGE_SIDE)), Center.WIDGE_TOP, Center.WIDGE_WIDTH , TreeOpts.HEIGHT_OF_OPTIONS);
        tree.setBounds(30, Center.WIDGE_TOP, Center.WIDGE_WIDTH, 40);
        search.setBounds(30, (segl2.getHeight() - (HeatMap.HEIGHT_OF_HEATMAP + 120)),Center.WIDGE_WIDTH,30);
        setFilterLVL_label();
        cf.setBounds((segl2.getWidth() - (Center.WIDGE_WIDTH + Center.WIDGE_SIDE)), (segl2.getHeight() - (HeatMap.HEIGHT_OF_HEATMAP + 130)), Center.WIDGE_WIDTH, CurrFilter.HEIGHT_OF_FILTER);
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if(source == ct){
            if(Menu.curJFrame!=null){Menu.curJFrame.dispose();}
            Menu.curJFrame = new FullT(center, segl2.getSegl1());
        }
    }
}